package com.robot.orders.service;

import java.util.ArrayList;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import util.IdWorker;

import com.robot.orders.dao.OrdersDetailsDao;
import com.robot.orders.pojo.OrdersDetails;

/**
 * details服务层
 *
 * @author wuyunbin
 */
@Service
public class OrdersDetailsService {
    @Autowired
    private OrdersDetailsDao detailsDao;

    @Autowired
    private IdWorker idWorker;

    /**
     * 查询全部列表
     *
     * @return
     */
    public List<OrdersDetails> findAll() {
        return detailsDao.findAll();
    }


    /**
     * 条件查询+分页
     *
     * @param whereMap
     * @param page
     * @param size
     * @return
     */
    public Page<OrdersDetails> findSearch(Map whereMap, int page, int size) {
        Specification<OrdersDetails> specification = createSpecification(whereMap);
        PageRequest pageRequest = PageRequest.of(page - 1, size);
        return detailsDao.findAll(specification, pageRequest);
    }


    /**
     * 条件查询
     *
     * @param whereMap
     * @return
     */
    public List<OrdersDetails> findSearch(Map whereMap) {
        Specification<OrdersDetails> specification = createSpecification(whereMap);
        return detailsDao.findAll(specification);
    }

    /**
     * 根据ID查询实体
     *
     * @param id
     * @return
     */
    public OrdersDetails findById(String id) {
        return detailsDao.findById(id).get();
    }

    /**
     * 增加
     *
     * @param details
     */
    public void add(OrdersDetails details) {
        //雪花分布式ID生成器
        details.setId(idWorker.nextId() + "");
        detailsDao.save(details);
    }

    /**
     * 修改
     *
     * @param details
     */
    public void update(OrdersDetails details) {
        detailsDao.save(details);
    }

    /**
     * 删除
     *
     * @param id
     */
    public void deleteById(String id) {
        detailsDao.deleteById(id);
    }

    /**
     * 动态条件构建
     *
     * @param searchMap
     * @return
     */
    private Specification<OrdersDetails> createSpecification(Map searchMap) {
        return new Specification<OrdersDetails>() {
            @Override
            public Predicate toPredicate(Root<OrdersDetails> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> predicateList = new ArrayList<Predicate>();
                // 结算明细id
                String id = "id";
                if (searchMap.get(id) != null && !"".equals(searchMap.get(id))) {
                    predicateList.add(cb.like(root.get(id).as(String.class), "%" + (String) searchMap.get(id) + "%"));
                }
                // 代理商id
                String agentId = "agentId";
                if (searchMap.get(agentId) != null && !"".equals(searchMap.get(agentId))) {
                    predicateList.add(cb.like(root.get(agentId).as(String.class), "%" + (String) searchMap.get(agentId) + "%"));
                }
                // 会员id
                String memberId = "memberId";
                if (searchMap.get(memberId) != null && !"".equals(searchMap.get(memberId))) {
                    predicateList.add(cb.like(root.get(memberId).as(String.class), "%" + (String) searchMap.get(memberId) + "%"));
                }
                // 商家id
                String companyId = "companyId";
                if (searchMap.get(companyId) != null && !"".equals(searchMap.get(companyId))) {
                    predicateList.add(cb.like(root.get(companyId).as(String.class), "%" + (String) searchMap.get(companyId) + "%"));
                }
                // 店铺id
                String storeId = "storeId";
                if (searchMap.get(storeId) != null && !"".equals(searchMap.get(storeId))) {
                    predicateList.add(cb.like(root.get(storeId).as(String.class), "%" + (String) searchMap.get(storeId) + "%"));
                }
                // 会员等级
                String memberGrade = "memberGrade";
                if (searchMap.get(memberGrade) != null && !"".equals(searchMap.get(memberGrade))) {
                    predicateList.add(cb.like(root.get(memberGrade).as(String.class), "%" + (String) searchMap.get(memberGrade) + "%"));
                }
                //剔除软删除数据
                String deletedAt = "deletedAt";
                predicateList.add(cb.equal(root.get(deletedAt), cb.nullLiteral(LocalDateTime.class)));

                return cb.and(predicateList.toArray(new Predicate[predicateList.size()]));
            }
        };
    }

}
